<template>
  <div class="cesium-draw">
    <div id="cesium-drawer" />
    <div class="mouse-postion">{{ nowPostion.wgs84 }}</div>
    <div class="tool-box">
      <div class="slider-box-wrapper"><slider-box v-model:value="terrainExaggeration" :max="2" /></div>
      <div class="slider-box-wrapper"><slider-box v-model:value="relativeHeight" :max="10000" /></div>
      <div id="menu">
        <p><button @click="onClick('play')">播放</button></p>
        <p><button @click="onClick('stop')">暂停</button></p>
      </div>
    </div>
  </div>
</template>

<script>
//参考：Cesium深入浅出之图层管理器 https://blog.csdn.net/fywindmoon/article/details/113747843
import { DownOutlined, EditOutlined } from '@ant-design/icons-vue';
import { defineComponent, ref, watch } from 'vue';
import SliderBox from '@components/widgets/slider-box';
import * as Cesium from '@cesium/Source/Cesium.js';
import MousePosition from '@functional/utils/mousePosition';

export default defineComponent({
  name: 'terrainExaggeration', // 地形夸张
  components: { DownOutlined, EditOutlined, SliderBox },
  setup() {
    return {
      viewer: null
    };
  },
  data() {
    return {
      terrainExaggeration: 1,
      relativeHeight: 2400,
      updateExaggeration: () => {},
      nowPostion: {},
      stopTime: undefined
    };
  },
  computed: {},
  watch: {
    terrainExaggeration(val) {
      this.updateExaggeration();
    },
    relativeHeight(val) {
      this.updateExaggeration();
    }
  },
  mounted() {
    this.initViewer();
  },
  beforeUnmount() {
    if (this.viewer) {
      this.viewer.destroy();
      this.viewer = null;
    }
  },
  methods: {
    onViewerInited(viewer) {
      let vm = this;
      new MousePosition(viewer, pos => {
        vm.nowPostion = pos;
      });
    },

    initViewer() {
      let vm = this;
      Cesium.Ion.defaultAccessToken = $accessToken.default;
      let osm = new Cesium.OpenStreetMapImageryProvider({
        url: 'https://a.tile.openstreetmap.org/'
      });
      let viewer = new Cesium.Viewer('cesium-drawer', {
        // imageryProvider: osm,
        terrainProvider: Cesium.createWorldTerrain(),
        contextOptions: {
          webgl: {
            alpha: true
          }
        },
        selectionIndicator: false,
        animation: false, //是否显示动画控件
        baseLayerPicker: false, //是否显示图层选择控件
        geocoder: false, //是否显示地名查找控件
        // timeline: false, //是否显示时间线控件
        sceneModePicker: false, //是否显示投影方式控件
        navigationHelpButton: false, //是否显示帮助信息控件
        infoBox: false, //是否显示点击要素之后显示的信息
        fullscreenButton: false,
        shouldAnimate: true
      });

      this.viewer = viewer;
      this.onViewerInited(viewer);

      this.initEffects();

      // this.viewer.flyTo(this.viewer.entities);
    },

    onClick(value) {
      switch (value) {
        case 'play':
          this.stratPlay();
          break;
        case 'stop':
          this.stopPlay();
          break;
      }
    },

    initEffects() {
      const viewer = this.viewer;

      var scene = viewer.scene;
      var globe = scene.globe;
      globe.terrainExaggeration = 1.0;
      globe.terrainExaggerationRelativeHeight = 2400.0;

      scene.camera.setView({
        destination: new Cesium.Cartesian3(336567.0354790703, 5664688.047602498, 2923204.3566963132),
        orientation: new Cesium.HeadingPitchRoll(1.2273281382639265, -0.32239612370237514, 0.0027207329018610338)
      });

      viewer.entities.add({
        position: new Cesium.Cartesian3(314557.3531714575, 5659723.771882165, 2923538.5417330978),
        ellipsoid: {
          radii: new Cesium.Cartesian3(400.0, 400.0, 400.0),
          material: Cesium.Color.RED,
          heightReference: Cesium.HeightReference.CLAMP_TO_GROUND
        }
      });

      var visualizeRelativeHeight = true;

      function updateMaterial() {
        if (visualizeRelativeHeight) {
          var height = globe.terrainExaggerationRelativeHeight;
          var exaggeration = globe.terrainExaggeration;
          var alpha = Math.min(1.0, exaggeration * 0.25);
          var layer = {
            extendUpwards: true,
            extendDownwards: true,
            entries: [
              {
                height: height + 100.0,
                color: new Cesium.Color(0.0, 0.0, 0.0, alpha * 0)
              },
              {
                height: height + 50.0,
                color: new Cesium.Color(1.0, 1.0, 1.0, alpha * 0.5)
              },
              {
                height: height,
                color: new Cesium.Color(1.0, 1.0, 1.0, alpha)
              },
              // {
              //   height: height - 10.0,
              //   color: new Cesium.Color(1.0, 1.0, 1.0, alpha * 0.5)
              // },
              {
                height: height - 11.0,
                color: new Cesium.Color(0.0, 0.0, 1.0, 0.6)
              }
            ]
          };
          scene.globe.material = Cesium.createElevationBandMaterial({
            scene: scene,
            layers: [layer]
          });
        } else {
          scene.globe.material = undefined;
        }
      }
      updateMaterial();

      this.updateExaggeration = function() {
        globe.terrainExaggeration = Number(this.terrainExaggeration);
        globe.terrainExaggerationRelativeHeight = Number(this.relativeHeight);
        updateMaterial();
      };

      // this.onClick('Satellites');
    },

    stratPlay() {
      const viewer = this.viewer;
      if (((viewer.clock.shouldAnimate = !0), this.stopTime)) {
        viewer.clock.currentTime = this.stopTime;
      } else {
        // var e = '2019-06-18';
        const t = new Date(),
          i = '00',
          a = '24',
          r = new Date(new Date(t).setHours(Number(i))),
          o = new Date(new Date(t).setHours(Number(a)));
        (viewer.scene.globe.enableLighting = !0),
          (viewer.shadows = !0),
          (viewer.clock.startTime = Cesium.JulianDate.fromDate(r)),
          (viewer.clock.currentTime = Cesium.JulianDate.fromDate(r)),
          (viewer.clock.stopTime = Cesium.JulianDate.fromDate(o)),
          (viewer.clock.clockRange = Cesium.ClockRange.LOOP_STOP),
          (viewer.clock.clockStep = Cesium.ClockStep.SYSTEM_CLOCK_MULTIPLIER),
          (viewer.clock.multiplier = 1600);
      }
    },

    stopPlay() {
      const viewer = this.viewer;
      (this.stopTime = viewer.clock.currentTime), (viewer.clock.shouldAnimate = !1);
    }
  }
});
</script>

<style scoped lang="less">
.cesium-draw {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  color: white;
  font-size: 14px;

  #cesium-drawer {
    width: 100%;
    height: 100%;
    overflow: hidden;
    :deep(.cesium-viewer-bottom) {
      display: none !important;
    }
  }

  .mouse-postion {
    position: absolute;
    bottom: 10px;
    right: 10px;
    font-size: 14px;
    padding: 8px 12px;
    line-height: 16px;
    color: white;
    z-index: 9999;
    background-color: rgba(40, 48, 60, 1);
    border-radius: 3px;
    opacity: 0.8;
  }
}

.chart-container {
  position: absolute;
  bottom: 7px;
  left: 7px;
  background-color: rgba(0, 0, 0, 0.5);

  #profileChart {
    width: 600px;
    height: 300px;
  }
}
#canvas-a {
  top: 10px;
  display: none;
}

#canvas-b {
  top: 120px;
  display: none;
}

.tool-box {
  position: absolute;
  top: 70px;
  left: 10px;
  z-index: 9;

  #menu {
    padding-top: 20px;
    button {
      background-color: rgba(0, 0, 0, 0.75);
      border-color: rgba(0, 0, 0, 0);
      cursor: pointer;
      transition: all 500ms;
      &:hover {
        opacity: 0.75;
      }
    }
  }

  .slider-box-wrapper {
    width: 20em;
    padding: 0 10px;
    background-color: rgba(0, 0, 0, 0.75);
    margin-bottom: 10px;
  }
}
</style>
